package com.zsinfo.guoss.supplier.activity;

import android.support.annotation.NonNull;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.netcenter.Event.Response;
import com.example.netcenter.RetrofitBuilder;
import com.example.netcenter.Url.ConstantsCode;
import com.example.netcenter.bean.BillingBean;
import com.example.netcenter.bean.ErrorBean;
import com.example.netcenter.bean.ResultsData;
import com.google.gson.Gson;
import com.zsinfo.guoss.supplier.R;
import com.zsinfo.guoss.supplier.base.BaseActivity;
import com.zsinfo.guoss.supplier.customize.AlertDialog;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by lixl on 2018/12/5.
 * <p>
 * desc:账单详情
 */
public class BillingDetailsActivity extends BaseActivity {

    @BindView(R.id.tv_type)
    TextView tv_type;
    @BindView(R.id.tv_money)
    TextView tv_money;

    @BindView(R.id.rcv_billing)
    RecyclerView rcv_billing;

    private List<String> titles = new ArrayList<>();
    private List<String> contents = new ArrayList<>();

    @Override
    protected int getContentLayoutRes() {
        return R.layout.activity_billing_details;
    }

    @Override
    protected void initView() {
        setTitle("账单详情");
        setBackAndLeftTitle("").setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }

    @Override
    protected void initNetData() {
        String id = getIntent().getStringExtra("id");
        GetNetCenter().GetNetCenter(this,
                RetrofitBuilder.CreateApi().SUPPLIER_BILL_DETAILS(ConstantsCode.supplier_bill_details, id), new Response() {
                    @Override
                    public void OK(Object data) {
                        Log.e("lixl", data.toString());
                        setView(data);
                    }

                    @Override
                    public void Fail(Throwable e) {
                        Log.e("lixl", e.getMessage());
                        String msg = "";
                        if (e.getMessage().contains("Code")) {
                            ErrorBean errorBean = new Gson().fromJson(e.getMessage(), ErrorBean.class);
                            msg = errorBean.getMsg();
                        } else {
                            msg = e.getMessage();
                        }
                        new AlertDialog(BillingDetailsActivity.this)
                                .builder()
                                .setGone()
                                .setMsg(msg)
                                .setPositiveButton("确定", new View.OnClickListener() {
                                    @Override
                                    public void onClick(View v) {
                                        finish();
                                    }
                                })
                                .show();
                    }

                    @Override
                    public void complete() {

                    }
                });
    }

    @Override
    protected void customDestroy() {

    }

    private void setView(Object data){
        ResultsData<BillingBean> resultsData = (ResultsData<BillingBean>) data;
        BillingBean billingBean = resultsData.getData();

        int type = billingBean.getType();
        int money = billingBean.getMoney();
        int billType = billingBean.getBillType();
        int status = billingBean.getStatus();
        if (type == 1){
            //缴纳
            tv_type.setText("缴纳保证金(元)");
            tv_money.setText("+" + money);

            titles.add("付款方式");
            titles.add("资金去向");
            titles.add("交易流水号");
            titles.add("创建时间");
            titles.add("到账时间");

            contents.add(billingBean.getPayMethod());
            if (billType == 1){
                contents.add("国产类保证金");
            } else if (billType == 2){
                contents.add("进口类保证金");
            }
            contents.add(billingBean.getBillNo());
            contents.add(billingBean.getCreateTime());
            contents.add(billingBean.getCreateTime());

        } else if (type == 2){
            //退款
            tv_type.setText("退款保证金(元)");
            tv_money.setText("-" + money + "元");

            titles.add("退款来源");
            titles.add("资金去向");
            titles.add("交易状态");
            titles.add("交易流水号");
            titles.add("创建时间");
            titles.add("到账时间");

            if (billType == 1){
                contents.add("国产类保证金");
            } else if (billType == 2){
                contents.add("进口类保证金");
            }
            contents.add(billingBean.getCapitalFlows());
            if (status == 1){
                contents.add("待处理");
            } else if (status == 2){
                contents.add("已完成");
            } else if (status == -1){
                contents.add("处理失败");
            }
            contents.add(billingBean.getBillNo());
            contents.add(billingBean.getCreateTime());
            contents.add(billingBean.getCreateTime());

        } else if (type == 3){
            //扣款
            tv_type.setText("扣款保证金(元)");
            tv_money.setText("-" + money + "元");

            titles.add("扣款项");
            titles.add("扣款原因");
            titles.add("交易流水号");
            titles.add("创建时间");

            contents.add(billingBean.getPayMethod());
            if (billType == 1){
                contents.add("国产类保证金");
            } else if (billType == 2){
                contents.add("进口类保证金");
            }
            contents.add(billingBean.getReason());
            contents.add(billingBean.getBillNo());
            contents.add(billingBean.getCreateTime());
        }

        rcv_billing.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        rcv_billing.setAdapter(new MyAdapter());
    }

    /**
     * 适配器
     */
    private class MyAdapter extends RecyclerView.Adapter<RecyclerHolder> {

        @NonNull
        @Override
        public RecyclerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(BillingDetailsActivity.this).inflate(R.layout.item_billing_details, parent, false);
            return new RecyclerHolder(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerHolder  holder, int position) {
            holder.tv_desc.setText(titles.get(position));
            holder.tv_content.setText(contents.get(position));
        }

        @Override
        public int getItemCount() {
            return titles.size();
        }
    }

    class RecyclerHolder extends RecyclerView.ViewHolder {
        TextView tv_desc;
        TextView tv_content;

        private RecyclerHolder(View itemView) {
            super(itemView);
            tv_desc = (TextView) itemView.findViewById(R.id.tv_desc);
            tv_content = (TextView) itemView.findViewById(R.id.tv_content);
        }
    }
}
